home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / wmv12s.zip / RM.C < prev    next >
Text File  |  1993-01-04  |  7KB  |  289 lines

  1. /* rm - removes everything you got
  2. ** Written by Peter Wu @ Faculty Support Center, MACC
  3. ** University of Wisconsin - Madison. August 1986.
  4. */
  5. #include "dta.h"
  6. #include <conio.h>
  7. #include <ctype.h>
  8. #include "date.h"
  9.  
  10. char getkey();
  11.  
  12. main(argc, argv)
  13. int argc;
  14. char *argv[];
  15. {
  16.   int brgc, optc, i, status, mask;
  17.   char *brgv[40], opt[26], *t, path[200], c;
  18.  
  19.   optc = 0;
  20.   brgc = 0;  /* my argc */
  21.  
  22.   for (i=0; i < 26; i++) {  /* clear options flags */
  23.     opt[i] = 0;
  24.   };
  25.  
  26.   for (i=1; i < argc; i++) {  /* separate options and arguments */
  27.     if ((*argv[i] == '/') || (*argv[i] == '-')) {
  28.       t = argv[i] + 1;
  29.       while ((c = *t++) != '\0') {
  30.     if (isalpha(c)) {
  31.       opt[tolower(c) - 'a'] = 1;
  32.     };
  33.       };
  34.     } else {
  35.       brgv[brgc++] = argv[i];
  36.     };
  37.   };
  38.  
  39.   if (brgc == 0) {
  40.     putn("Usage: RM <filespec>\15\n",
  41.      "<filespec> can have wild cards and attributes /f /d /h; E.g:\15\n",
  42.      "  *.* or *.*/f  selects all visible file\15\n",
  43.      "  *.*/h or *.*/hf  selects all visible and invisible files\15\n",
  44.      "  *.*/d  selects all sub-directories\15\n\n",
  45.      "For each file/sub-directory found, you will be prompted for\15\n",
  46.      "one of the following actions:\15\n",
  47.      "   y - yes, delete this file\15\n",
  48.      "   Y - Yes, delete this sub-directory\15\n",
  49.      "   n - no, leave this file/sub-directory alone\15\n",
  50.      "   l - list all the rest of the files/sub-directories\15\n",
  51.      "   Z - delete ALL the rest of the files/sub-directories\15\n",
  52.      "   e - enter this sub-directory\15\n",
  53.      "   x - exit current sub-directory\15\n",
  54.      "   q - quit now\15\n\n",
  55.      "Version 2.00 made ", date, ".\15\n",
  56.      "Please report problems to Peter Wu.\15\n",
  57.      0);
  58.     exit(0);
  59.   }
  60.  
  61.   /* process parameters */
  62.   for (i=0; i < brgc; i++) {
  63.     strcpy(path, brgv[i]);
  64.     mask = extype(path);
  65.     mask |= normal(path);
  66.     if ((mask & (A_FIL | A_DIR)) == 0) {
  67.       mask |= A_FIL;  /* default is remove files only */
  68.     }
  69.     rm(1, 1, mask, path);
  70.   }
  71. }
  72.  
  73. rm(confirm, echo, mask, path)  /* use normalized path only */
  74. int confirm, mask;
  75. char *path;
  76. {
  77.   char headp[200], fullp[200], c;
  78.   int status;
  79.   union dtbuf mydta;
  80.  
  81.   strcpy(headp,path);
  82.   chopath(headp);  /* cut off the tail (might be wild-cards) */
  83.  
  84.   status = ffmf(path, mask, &mydta);
  85.   if (status) {  /* not found; probably empty directory */
  86.     return 1;  /* let the caller print out the error message */
  87.   }
  88.  
  89.   do {    /* for all matching files */
  90.  
  91.     strcpy(fullp, headp);
  92.     catpath(fullp, mydta.dos.fn);  /* now fullp has the expanded name */
  93.  
  94.     do {
  95.  
  96.       if (confirm | echo) {  /* echo files to be deleted */
  97.     cputs(fullp);
  98.     if (mydta.dos.attr & A_DIR) {
  99.       cputs(" <DIR>");
  100.     }
  101.     if (mydta.dos.attr & (A_HID | A_SYS)) {
  102.       cputs(" (hidden)");
  103.     }
  104.       }
  105.  
  106.       if (confirm) {  /* ask for confirmation */
  107.  
  108.     status = 0;
  109.     if (mydta.dos.attr & A_DIR) {  /* sub-dir */
  110.       cputs(" (Y/n/e/x/l/Z/q; ? for help): ");
  111.       c = getkey("YynlxqeZ?");
  112.     } else {  /* file */
  113.       cputs(" (y/n/x/l/Z/q; ? for help): ");
  114.       c = getkey("YynlxqZx?");
  115.     }
  116.  
  117.     switch (c) {
  118.  
  119.       case '?':
  120.         putn("\15\n",
  121.          "Y - Yes, delete this sub-directory and its content\15\n",
  122.          "y - yes, delete this file\15\n",
  123.          "n - no, skip this file/sub-directory\15\n",
  124.          "e - enter this sub-directory\15\n",
  125.          "x - exit current sub-directory\15\n",
  126.          "l - list all the rest of the files/sub-directories\15\n",
  127.          "Z - delete ALL the rest without further prompts\15\n",
  128.          "q - quit to DOS now\15\n",
  129.          0);
  130.          cputs("\n");
  131.         break;
  132.  
  133.       case 'Z':
  134.         putn("\15\n",
  135.            "Really delete ALL the remaning files/sub-directories (Y/N)? ",
  136.            0);
  137.         c = getkey("YNn");
  138.         if (c == 'Y') {
  139.           confirm = 0;  /* no more confirmation request */
  140.           status = 1;  /* exit loop */
  141.         }
  142.         cputs("\15\n");
  143.         break;
  144.  
  145.       case 'q':
  146.         cputs("\15\nquiting to DOS\15\n");
  147.         exit(0);
  148.  
  149.       case 'x':
  150.         cputs("\15\n");
  151.         return 2;  /* exit current directory */
  152.  
  153.       case 'y':
  154.         if (mydta.dos.attr & A_DIR) { /* can't delete sub-dir with 'y' */
  155.           cputs(" use Y to delete sub-directory\15\n");
  156.         } else {  /* delete this file */
  157.           putch(' ');  /* move the cursor */
  158.           status = 1;  /* delete */
  159.         }
  160.         break;
  161.  
  162.       case 'Y':
  163.         if (mydta.dos.attr & A_DIR) {
  164.           putch(' ');  /* move the cursor */
  165.           status = 1;  /* delete */
  166.         } else {  /* can't delete file with 'Y' */
  167.           cputs(" use y to delete file\15\n");
  168.         }
  169.         break;
  170.  
  171.       case 'n':
  172.         status = 2;
  173.         break;
  174.  
  175.       case 'l':
  176.         cputs("\15\n");
  177.         ls(mydta);
  178.         break;
  179.  
  180.       case 'e':
  181.         cputs(" entering sub-directory\15\n\n");
  182.         catpath(fullp,"*.*");
  183.         if (rm(1,1,A_MASK,fullp) == 1) {  /* empty */
  184.           cputs("This sub-directory is empty. ");
  185.         }
  186.         chopath(fullp);
  187.         cputs("Returning to previous directory.\15\n\n");
  188.         break;
  189.  
  190.       default:  /* what did I miss? */
  191.         cputs("invalid key\15\n");
  192.     }
  193.  
  194.       } else {    /* confirm == 0 */
  195.     status = 1;
  196.       }
  197.  
  198.     } while (!status);
  199.  
  200.     /* status == 1 delete; status == 2 don't delete */
  201.  
  202.     if (status == 1) {    /* delete */
  203.       if (mydta.dos.attr & A_DIR) {  /* delete sub-directory */
  204.     /* check if fullp is parent of current path */
  205.  
  206.     /* call rm recursively to remove stuff */
  207.     catpath(fullp,"*.*");
  208.     rm(0,0,A_MASK,fullp);  /* no confirmation for this */
  209.     chopath(fullp);  /* cut off the "*.*" */
  210.     status = rmdir(fullp);
  211.     if (echo || confirm) {
  212.       if (status) {
  213.         cputs(" can't delete\15\n");
  214.       } else {
  215.         cputs(" Deleted\15\n");
  216.       }
  217.     }
  218.  
  219.       } else {    /* delete files */
  220.  
  221.     status = unlink(fullp);
  222.     if (echo || confirm) {
  223.       if (status) {
  224.         cputs(" can't delete\15\n");
  225.       } else {
  226.         cputs(" deleted\15\n");
  227.       }
  228.     }
  229.       }
  230.  
  231.     } else {
  232.       cputs(" not deleted\15\n");
  233.     }
  234.  
  235.     status = fnmf(&mydta);
  236.   } while (status == 0);
  237.  
  238.   return 0;
  239. }
  240.  
  241. ls(cdta)  /* list the rest of the files */
  242. union dtbuf cdta;
  243. {
  244.   int i, status, col, slen;  /* column */
  245.  
  246.   col = 0;
  247.   do {    /* list the current one also */
  248.     col++;
  249.     slen = strlen(cdta.dos.fn);
  250.     cputs(cdta.dos.fn);
  251.     if (cdta.dos.attr & A_DIR) {
  252.       putch('\\');
  253.       slen++;
  254.     };
  255.  
  256.     if (col < 5) {
  257.       for (i=slen; i < 16; i++) {
  258.     putch(' ');
  259.       }
  260.     } else {
  261.       cputs("\15\n");
  262.       col = 0;
  263.     }
  264.     status = fnmf(&cdta);
  265.   } while (!status);
  266.  
  267.   if (col) {  /* complete last line */
  268.     cputs("\15\n");
  269.   }
  270. }
  271.  
  272. char getkey(valid)  /* wait for valid key and echo it */
  273. char *valid;
  274. {
  275.   char c;
  276.   int i;
  277.  
  278.   do {
  279.     c = getch();
  280.     i = index(valid,c);
  281.     if (i > -1) {
  282.       putch(c);  /* echo valid key */
  283.       return c;
  284.     } else {  /* beep at invalid key */
  285.       putch(7);
  286.     }
  287.   } while (1);
  288. }
  289.